package com.gopay.bis.web.controller.ebank;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.yspay.ebank.YspayMessage;
/**
 * 银盛后台银行回应
 * 
 * @YsPayServerRes.java
 * @author lilili
 */
@Controller
@Scope("prototype")
public class YsPayServerRes extends BankResBase {
	 private static Log logger = LogFactory.getLog(YsPayServerRes.class);
		
    @RequestMapping(value = "/ysPayServerRes")
    protected ModelAndView execute(HttpServletRequest request, HttpServletResponse response) {
        logger.error("--------------- YsPayServerRes bankback  ---------------");
        return super.execute(request, response);
    }
    
	@Override
    protected BaseMessage buildMessage(HttpServletRequest request) {
        YspayMessage message = new YspayMessage();
        message.setBank(BankConstants.BANK_LOGO_YSPAY);
        message.setSrc(request.getParameter("src"));
        message.setMsgCode(request.getParameter("msgCode"));
        message.setMsg(request.getParameter("msg"));
        message.setSignMessage(request.getParameter("check"));
        // 银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
        // 银行接口通道信息 12: 银盛
        message.setBankIntrChann(BisBankInterfaceChannel._12);
        return message;
    }

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
	    YspayMessage message = (YspayMessage)result.getModel("res");
        OrderInfo orderInfo = new OrderInfo();
        if(message!=null){
            String retCode = message.getBusiState();
            String amount = TransUtils.amountConvert2Yuan(message.getOrderAmount());
            if(retCode!=null && retCode.equals("00")){
                orderInfo.setPayStatus("00");
                orderInfo.setRetCode("00");
                orderInfo.setRetMsg("");
            }else{
                orderInfo.setPayStatus("01");
                orderInfo.setRetCode("01");
                orderInfo.setRetMsg("");
            }
            orderInfo.setOrderId(message.getOrderId());
            orderInfo.setAmt(amount);
            orderInfo.setNotifyNum("1");
        }
        orderInfo.setBankName("yspay");
        orderInfo.setIsNotify(true);
        return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("src"))
                || StringUtils.isEmpty(request.getParameter("msgId"))
                || StringUtils.isEmpty(request.getParameter("msg"))
                || StringUtils.isEmpty(request.getParameter("check"))
                || StringUtils.isEmpty(request.getParameter("msgCode"))) {
        return false;
        }
        return true;
	}

}
